<template>
  <div>
    <div class="search">
      <el-select v-model="appstatus" placeholder="请选择状态查询" style="width: 200px ;margin-left:5px">
        <el-option label="待审核" value="待审核"></el-option>
        <el-option label="未通过" value="未通过"></el-option>
        <el-option label="已通过" value="已通过"></el-option>

      </el-select>
      <el-button type="info" plain style="margin-left: 10px" @click="load(1)">查询</el-button>
      <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
    </div>
    <div class="table">
      <el-table :data="tableData" stripe>
        <el-table-column label="序号" width="80" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="赛事名称"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="level" label="级别" show-overflow-tooltip></el-table-column>
        <el-table-column prop="teacherName" label="教师" show-overflow-tooltip></el-table-column>
        <el-table-column prop="status" label="竞赛状态" show-overflow-tooltip></el-table-column>
        <el-table-column prop="appstatus" label="审核状态"></el-table-column>
        <el-table-column prop="note" label="审核说明"></el-table-column>
        <el-table-column prop="studentName" label="队长姓名"></el-table-column>
        <el-table-column prop="teammate" label="队员姓名"></el-table-column>
        <el-table-column label="操作" width="250" align="center">
          <template v-slot="scope">
            <el-button plain type="primary" @click="handleEdit(scope.row)" size="mini" v-if="user.role==='ADMIN'">编辑</el-button>
            <el-button plain type="primary" @click="handleEdit(scope.row)" size="mini" v-if="user.role==='TEACHER'">审核</el-button>
            <el-button plain type="primary" @click=del(scope.row.id) size="mini" v-if="user.role==='STUDENT'">取消报名</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
                background
                @current-change="handleCurrentChange"
                :current-page="pageNum"
                :page-sizes="[5, 10, 20]"
                :page-size="pageSize"
                layout="total, prev, pager, next"
                :total="total">
        </el-pagination>
      </div>
    </div>

    <el-dialog name="信息" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
      <el-form label-width="100px" style="padding-right: 50px" :model="form" :rules="rules" ref="formRef">
        <!--        这里真TMD坑，表单的名称不能和数据库的一样，不然数据库插入不成功-->
<!--        大赛名字和级别建立后不允许改变-->
        <el-form-item prop="name" label="赛事名称">
          <el-input v-model="form.name" autocomplete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item prop="level" label="级别">
          <el-input v-model="form.level" autocomplete="off"  :disabled="true"></el-input>
        </el-form-item>

<!--        教师只有管理员可以改变-->
        <el-form-item prop="teacherId" label="教师">
          <el-select v-model="form.teacherId" placeholder="请选择教师" style="width: 100%" :disabled="user.role!=='ADMIN'">
            <el-option v-for="item in teacherData" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

<!--        当前比赛进度，审核进度，审核说明，只有具体老师能修改-->
        <el-form-item prop="status" label="当前比赛进度" >
          <el-select v-model="form.status" placeholder="请选择" style="width: 100%" :disabled="user.role!=='TEACHER'">
            <el-option label="报名通道未开启" value="报名通道未开启"></el-option>
            <el-option label="报名中" value="报名中"></el-option>
            <el-option label="报名结束，比赛进行中" value="报名结束,比赛进行中"></el-option>
            <el-option label="比赛已结束" value="比赛已结束"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="appstatus" label="审核状态">
          <el-select v-model="form.appstatus" placeholder="请选择" style="width: 100%" :disabled="user.role!=='TEACHER'">
            <el-option label="待审核" value="待审核"></el-option>
            <el-option label="未通过" value="未通过"></el-option>
            <el-option label="已通过" value="已通过"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="level" label="审核说明" >
          <el-input v-model="form.note" autocomplete="off" :disabled="user.role!=='TEACHER'"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="fromVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    name: "Apply",
    data() {
      return {
        tableData: [],  // 所有的数据
        pageNum: 1,   // 当前的页码
        pageSize: 10,  // 每页显示的个数
        total: 0,
        name:null,
        fromVisible: false,
        form:{},
        appstatus:null,
        user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
        rules: {
          name: [
            {required: true, message: '请输入竞赛名称', trigger: 'blur'},
          ],
        },
        ids: [],
        teacherData:[],
        contestData:[]
      }
    },
    created() {
      this.load(1)
      this.loadContest()
    },
    methods: {
      save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
        this.$refs.formRef.validate((valid) => {
          if (valid) {
            this.$request({
              url: this.form.id ? '/apply/update' : '/apply/add',
              method: this.form.id ? 'PUT' : 'POST',
              data: this.form
            }).then(res => {
              if (res.code === '200') {  // 表示成功保存
                this.$message.success('保存成功')
                this.load(1)
                this.fromVisible = false
              } else {
                this.$message.error(res.msg)  // 弹出错误的信息
              }
            })
          }
        })
      },
      handleEdit(row) {   // 编辑数据
        this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
        this.fromVisible = true   // 打开弹窗
      },
      del(id) {   // 单个删除
        this.$confirm('您确定取消报名吗？', '提醒', {type: "warning"}).then(response => {
          this.$request.delete('/apply/delete/' + id).then(res => {
            if (res.code === '200') {   // 表示操作成功
              this.$message.success('操作成功')
              this.load(1)
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }).catch(() => {
        })
      },
      load(pageNum) {  // 分页查询
        if (pageNum) this.pageNum = pageNum
        this.$request.get('/apply/selectPage', {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.name,
            appstatus: this.appstatus,
          }
        }).then(res => {
          this.tableData = res.data?.list
          this.total = res.data?.total
        })
      },
      reset() {
        this.name = null
        this.appstatus = null
        this.load(1)
      },
      handleCurrentChange(pageNum) {
        this.load(pageNum)
      },
    },
    loadContest() {
      this.$request.get('/contest/selectAll').then(res => {
        if (res.code === '200') {
          this.contestData = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  }
</script>

<style scoped>

</style>
